<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-20 14:16:12
 * @LastEditors: wangming
 * @LastEditTime: 2022-02-10 10:28:04
 * @Description: file content
-->
<template>
  <div>
    <el-row :gutter="12">
      <el-col :span="6">
        <el-input
          v-model="pageData.searchKey"
          placeholder="请输入关键字"
          clearable
          :maxlength="64"
          prefix-icon="el-icon-search"
        />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="loadData(1)">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-col>
      <el-col class="mt10">
        <el-button type="primary" @click="handleCreate">新建</el-button>
        <CommonImport
          ref="CommonImport"
          class="ml10 mr10"
          style="display: inline-block"
          :importApi="postHmi18ninfoImport"
          :importUrl="`/api/hm-i18n-info/import`"
          @ok="handleImportOk"
        >
          <el-button-group>
            <el-button type="primary">导入</el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="翻译模板下载"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-download"
                @click.stop="handleDownLoad"
              ></el-button>
            </el-tooltip>
          </el-button-group>
        </CommonImport>
        <el-button type="primary" @click.stop="handleExport">导出</el-button>
      </el-col>
    </el-row>
    <el-table
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
    >
      <el-table-column prop="key" label="资源标识" fixed="left" />
      <el-table-column prop="key" label="资源类型" fixed="left">
        <template slot-scope="scope">
          <span>{{scope.row.type == 'CUSTOM' ? '自定义' : '系统'}}</span>
        </template>
      </el-table-column>
      <template v-for="item in columnList">
        <el-table-column
          :key="item.key"
          :prop="item.key"
          :label="item.region"
        />
      </template>
      <el-table-column prop="actions" label="操作" fixed="right">
        <template slot-scope="scope">
          <template>
            <span class="table-actionItem"
              ><el-button type="text" @click="handleEdit(scope.row)"
                >编辑</el-button
              ></span
            >
            <span class="table-actionItem" v-if="scope.row.type == 'CUSTOM'">
              <el-popconfirm
                title="确定要删除该翻译吗？"
                placement="top"
                @confirm="handleDelete(scope.row)"
              >
                <el-button class="link-danger" type="text" slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
            </span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="tr mt24">
      <x-pagination
        :total="total"
        :pageSize.sync="pageData.pageSize"
        :pageNo.sync="pageData.pageNo"
        @search="loadData"
      />
    </div>
  </div>
</template>

<script>
import CommonImport from "@/components/CommonImport";
import { downloadFileStream } from "@/utils/util";
import TranslateStageModal from "./translateStageModal/index";
import {
  getHmi18ninfoPageList,
  deleteHmi18ninfo,
  putHmi18ninfo,
  postHmi18ninfo,
  getHmi18ninfoXlsxtemplate,
  getHmi18ninfoExport,
  postHmi18ninfoImport,
} from "@/apis/paas/HmI18nInfoController.js";
import { getHmi18nlocaleList } from "@/apis/paas/HmI18nLocaleController.js";
export default {
  name: "TranslateStage",
  components: {
    CommonImport,
    TranslateStageModal,
  },
  data() {
    return {
      loading: false,
      pageData: {
        pageSize: 10,
        pageNo: 1,
        searchKey: "",
      },
      tableData: [],
      columnList: [],
      total: 0,
    };
  },

  created() {
    this.getColumns();
    this.loadData();
  },

  methods: {
    postHmi18ninfoImport,
    loadData(pageNo) {
      pageNo && (this.pageData.pageNo = pageNo);
      getHmi18ninfoPageList({
        ...this.pageData,
      }).then((res) => {
        this.tableData = res.data?.map((item) => {
          const localeObj = {};
          item.infoList.forEach((ele) => {
            localeObj[ele.locale] = ele.info;
          });
          return {
            key: item.key,
            type: item.type,
            ...localeObj,
          };
        });
        this.total = res.totalCount;
      });
    },
    getColumns() {
      getHmi18nlocaleList().then((res) => {
        this.columnList = res.map((item) => {
          return { key: item.key, region: item.region };
        });
      });
    },

    handleReset() {
      this.pageData = {
        pageSize: 10,
        pageNo: 1,
        searchKey: "",
      };
      this.loadData();
    },
    handleExport() {
      getHmi18ninfoExport({ responseType: "blob" }).then((res) => {
        let fileName = window.decodeURI(
          res.headers?.["content-disposition"].split("=")[1],
          "UTF-8"
        );
        downloadFileStream(res.data, fileName);
      });
    },
    handleImportOk() {
      this.$message.success("操作成功");
      this.loadData();
    },
    handleDownLoad() {
      getHmi18ninfoXlsxtemplate({ responseType: "blob" }).then((res) => {
        let fileName = window.decodeURI(
          res.headers?.["content-disposition"].split("=")[1],
          "UTF-8"
        );
        downloadFileStream(res.data, fileName);
        this.$message.success("操作成功");
      });
    },

    handleCreate() {
      TranslateStageModal.addForm(this.columnList).then((data) => {
        postHmi18ninfo(data).then((res) => {
          this.loadData();
        });
      });
    },

    handleEdit(record) {
      TranslateStageModal.editForm(this.columnList, {
        ...record,
      }).then((data) => {
        putHmi18ninfo(data).then((res) => {
          this.loadData();
        });
      });
    },

    handleDelete(record) {
      deleteHmi18ninfo([record.key]).then((res) => {
        this.loadData();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.upload-btn {
  display: inline-block;
  float: left;
}
</style>
